/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.modules.app.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.app.entity.BiaodiwuEntity;
import io.renren.modules.app.entity.BiaodiwufileEntity;
import io.renren.modules.app.entity.CategoryEntity;
import io.renren.modules.app.service.BiaodiwuService;
import io.renren.modules.app.service.BiaodiwufileService;
import io.renren.modules.app.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * APP登录授权
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/api/")
@Api("看样预约、资讯、贷款服务、办证服务、轮播图")
public class MainController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private BiaodiwuService biaodiwuService;

    @Autowired
    private BiaodiwufileService biaodiwufileService;

    @PostMapping("category/top")
    @ApiOperation("查询顶级的类别")
    public R categoryTops() {
        QueryWrapper<CategoryEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", "00").orderByAsc("sort");
        List<CategoryEntity> list = categoryService.list(queryWrapper);
        return R.ok(list);
    }

    @PostMapping("category/tree")
    @ApiOperation("查询顶级的类别")
    public R categoryTree() {
        return R.ok(categoryService.list());
    }

    @PostMapping("category/second/{pid}")
    @ApiOperation("根据顶级类别查询二级")
    public R categorySecond(@PathVariable String pid) {
        QueryWrapper<CategoryEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", pid).orderByAsc("sort");
        List<CategoryEntity> list = categoryService.list(queryWrapper);
        return R.ok(list);
    }


    @PostMapping("biaodiwu/query")
    @ApiOperation("根据编号或关键字查标的物列表")
    public R biaodiwuQuery(@RequestBody PramsDto1 param) {
        String keyword = param.getKeyword();
        QueryWrapper<BiaodiwuEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","number","title").eq("number",keyword).or().like("title",keyword);
        return R.ok(biaodiwuService.list(queryWrapper));
    }


    @PostMapping("biaodiwu/swiper")
    @ApiOperation("首页标的物轮播图")
    public R biaodiwuAppList() {
        List<List<BiaodiwuEntity>> list = new ArrayList<>();

        list.add(biaodiwuService.get12BiaodiwuByCategory("法拍房"));
        list.add(biaodiwuService.get12BiaodiwuByCategory("机动车"));
        list.add(biaodiwuService.get12BiaodiwuByCategory("综合拍品"));
        return R.ok(list);
    }
    @PostMapping("biaodiwu/applist")
    @ApiOperation("根据查询条件查询标的物，用于小程序类别展示页面")
    public R biaodiwuAppList(@RequestBody PramsDtoB prams) {

        QueryWrapper<BiaodiwuEntity> queryWrapper = new QueryWrapper<>();
        if (prams.getCategoryId() != null && prams.getCategoryId().trim().length() > 0) {
            List<CategoryEntity> categoryList = categoryService.queryCategoryAndChild(prams.getCategoryId());
            List<String> categoryIds = categoryList.stream().map(CategoryEntity::getId).collect(Collectors.toList());
            if (categoryIds.size() > 0) {
                queryWrapper.in("category_id", categoryIds);
            }
        }

        if (prams.getStatus() != -1) {
            if(prams.getStatus() == 5){
                //todo 状态为5 时，前端显示的是终止，实际上在后台对应多个值，是否全查出来
                queryWrapper.eq("status", 5)
                        .or()
                        .eq("status", 6)
                        .or()
                        .eq("status", 7);
            }else{
                queryWrapper.eq("status", prams.getStatus());
            }
        }

        if (prams.getChengshi() != null && prams.getChengshi().trim().length() > 0) {
            queryWrapper.like("chengshi", prams.getChengshi());
        }

        if (prams.getKeys() != null && prams.getKeys().trim().length() > 0) {
            queryWrapper.like("title", prams.getKeys());
        }

        if (prams.getSortFild() != null && prams.getSortFild().trim().length() > 0) {
            if (prams.getDesc() == 1) {
                queryWrapper.orderByDesc(prams.getSortFild());
            } else if (prams.getDesc() == 0) {
                queryWrapper.orderByAsc(prams.getSortFild());
            }
        }

        if (prams.getTime() !=0) {
            //todo 根据日期范围查询
//if (prams.getTime() ==3){}else if (prams.getTime() ==7){}else if (prams.getTime() ==10){}
            //3 天 7天 10天的日期范围
//            queryWrapper.between("create_time","","");
        }
        if(!prams.getDate().equals("_")){
//            String[] split = prams.getDate().split("_");
//            split[0] 2021-08-07      split[1] 2021-08-17
//            queryWrapper.between("start_time",split[0],split[1]);
        }

            IPage<BiaodiwuEntity> page = biaodiwuService.page(
                new Page<>(prams.getPage(), prams.getLimit()),
                queryWrapper
        );
        return R.ok(new PageUtils(page));
    }

    @PostMapping("biaodiwu/weblist")
    @ApiOperation("根据查询条件查询标的物，用于官网类别展示页面")
    public R biaodiwuWebList(@RequestBody PramsDtoB prams) {

        return biaodiwuAppList(prams);
    }


    //todo 分页有问题
    @PostMapping("biaodiwu/index-hot")
    @ApiOperation("用于小程序首页-最热标的物")
    public R biaodiwuIndexHot(@RequestBody PramsDto prams) {
        QueryWrapper<BiaodiwuEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hotIndex", 1);
        IPage<BiaodiwuEntity> page = biaodiwuService.page(
                new Page<>(prams.getPage(), prams.getLimit()),
                queryWrapper
//                new Query<BiaodiwuEntity>().getPage(prams.getPage(), prams.getLimit(), null, false),
//                queryWrapper
        );
        return R.ok(new PageUtils(page));
    }

    //todo 分页有问题
    @PostMapping("biaodiwu/index-new")
    @ApiOperation("用于小程序首页-最新标的物(根据start_time排序)")
    public R biaodiwuIndexNew(@RequestBody PramsDto prams) {
        QueryWrapper<BiaodiwuEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("start_time");
        IPage<BiaodiwuEntity> page = biaodiwuService.page(
                new Page<>(prams.getPage(), prams.getLimit()),
                queryWrapper
        );
        return R.ok(new PageUtils(page));
    }
    //todo 分页有问题
    @PostMapping("biaodiwu/category-hot")
    @ApiOperation("用于小程序首页-类别最热标的物")
    public R biaodiwuIndexCategoryHot(@RequestBody PramsDto prams) {
        if (prams.getCategoryId() != null) {
            List<CategoryEntity> categoryEntities = categoryService.queryCategoryAndChild(prams.getCategoryId());
            List<String> categoryIds = categoryEntities.stream().map(CategoryEntity::getId).collect(Collectors.toList());
            QueryWrapper<BiaodiwuEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("category_id", categoryIds).eq("hotCategroy", 1);
            IPage<BiaodiwuEntity> page = biaodiwuService.page(
                    new Page<>(prams.getPage(), prams.getLimit()),
                    queryWrapper
//                    new Query<BiaodiwuEntity>().getPage(prams.getPage(), prams.getLimit(), null, false),
//                    queryWrapper
            );
            return R.ok(new PageUtils(page));
        }
        return R.ok();
    }


    @GetMapping("biaodiwu/{id}")
    @ApiOperation("查看标的物详情")
    public R biaodiwuXiangqing(@PathVariable("id") String id) {
        BiaodiwuEntity biaodiwuEntity = biaodiwuService.getById(id);
        if (biaodiwuEntity != null) {
            List<BiaodiwufileEntity> biaodiwufileList = biaodiwufileService.list(new QueryWrapper<BiaodiwufileEntity>().eq("biaodiwu_id", biaodiwuEntity.getId()).orderByAsc("file_type", "sort"));
            if (biaodiwufileList.size() > 0) {
                for (BiaodiwufileEntity biaodiwufileEntity : biaodiwufileList) {
                    if (biaodiwufileEntity.getFileType() == 1) {
                        biaodiwuEntity.getImageFiles().add(biaodiwufileEntity);
                    } else if (biaodiwufileEntity.getFileType() == 2) {
                        biaodiwuEntity.getVideoFiles().add(biaodiwufileEntity);
                    } else if (biaodiwufileEntity.getFileType() == 3) {
                        biaodiwuEntity.getAdjunctFiles().add(biaodiwufileEntity);
                    }
                }
            }
            //设置 1评估价/2协议价/3询价
            if (biaodiwuEntity.getPriceShow() == 1) {
                biaodiwuEntity.setPriceShowName("评估价");
                biaodiwuEntity.setPriceShowValue(biaodiwuEntity.getPingguPrice());
            } else if (biaodiwuEntity.getPriceShow() == 2) {
                biaodiwuEntity.setPriceShowName("协议价");
                biaodiwuEntity.setPriceShowValue(biaodiwuEntity.getXieyiPrice());
            } else if (biaodiwuEntity.getPriceShow() == 3) {
                biaodiwuEntity.setPriceShowName("询价");
                biaodiwuEntity.setPriceShowValue(biaodiwuEntity.getXunPrice());
            }
        }

        return R.ok(biaodiwuEntity);
    }

    @Data
    public static class PramsDto1 {
        private String keyword;
    }

    @Data
    public static class PramsDto {
        private int page;
        private int limit;
        private String categoryId;
    }

    @Data
    public static class PramsDtoB {
        private int page;
        private int limit;
        private String sortFild;//"start_price"
        private int desc;// 0 asc  1 desc
        private String categoryId;
        private int status;
        private String chengshi;
        private String keys;
        private String  date;
        private int  time;
    }

    @Data
    public static class PramsDtoA {
        private int page;
        private int limit;
        private String sortFild;//"start_price"
        private int desc;// 0 asc  1 desc
        private String categoryId;
        private int status;
        private String chengshi;

    }


}
